package com.hai.controller;

import com.hai.response.Result;
import com.hai.service.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 控制器类
 */
@Api(value = "文件操作" , tags = "图像操作")
@RequestMapping("/oss")
@RestController
public class AliOssController {

    @Autowired
    private OssService ossService;

    @ApiOperation(value = "创建容器")
    @GetMapping("/ossService")
    public String ossService(){
        ossService.createBucket("aa");
        return "";
    }


    @ApiOperation(value = "文件上传")
    @PostMapping("uploadImgFile")
    public Result upload(MultipartFile file){
        String url = ossService.upload(file);
        return Result.ok().data("url",url);
    }

    @ApiOperation(value = "删除文件")
    @DeleteMapping("deleteImgFile")
    public Result deleteImgFile(String file){
        //https://xinguan-parent.oss-cn-hangzhou.aliyuncs.com/2020/09/25/1575345b2cd14c13872f9b83a0aac919.png
        try {
            String[] splitFile = file.split(".com/");
            ossService.deleteFile(splitFile[1]);
            return Result.ok();
        }catch (Exception e){
            return Result.error();
        }
    }
}
